package ESP.Control.C;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.struts2.json.annotations.JSON;

import ESP.Dao.BaseDao;
import ESP.VO.Faculty;
import ESP.VO.Major;
import ESP.VO.Role;

import com.alibaba.fastjson.JSONArray;
import com.opensymphony.xwork2.ActionSupport;

public class c01 extends ActionSupport{
	@Resource
	public BaseDao baseDao;
	/**
	 * 查询
	 */
	private static final long serialVersionUID = 1L;
	private String task_id;
	private String task_name;
	private String task_note;
	private String task_faculty_id;
	private String task_major_id;
	private int task_year;
	
	private int taskYearStart;
	private int taskYearEnd;
	/**
	 * 返回信息
	 */
	private Map<String, String> message = new HashMap<String, String>();;
	private JSONArray rows;
//	private List<Object> tasklist;
//	private List<Faculty> facultylist;
//	private List<Major> majorlist;
	
	/**
	 * 登录信息user_id
	 */
	private String user_id;
	private String user_role_id;
	/**
	 * 查询课题信息
	 * @return
	 */
	public String queryTask(){
		this.message = null;
		try {
			if (!(user_role_id.equals(Role.ADMIN) || user_role_id.equals(Role.TEACHER) || user_role_id == null)) {//不是管理员和教师
				//将查询条件中的专业、学院、学年指定
				Map<String, Object> map = baseDao.queryStu(user_id);
				this.task_faculty_id = map.get("stu_faculty_id").toString();
				this.task_major_id = map.get("stu_major_id").toString();
				int year = new Date().getYear()+1900;
				taskYearStart = year;
				taskYearEnd = year+1;
			}
			List list = baseDao.queryTask(task_id, task_name, task_note, task_faculty_id, task_major_id, taskYearStart, taskYearEnd);
			this.rows = baseDao.toJson(list);
			baseDao.setReturnInfo(message, "Y", "查询完毕");
			return "queryTask";
		} catch (Exception e) {
			e.printStackTrace();
			baseDao.setReturnInfo(message, "N", "查询失败");
			return "queryTask";
		}
	}
	/**
	 * 组长选课题
	 */
	public String confirmTask(){
		int i = baseDao.updateGroupTaskId(user_id, this.task_id);
		if (i == 0) {
			baseDao.setReturnInfo(message, "N", "选题失败!");
		}else if(i == 1){
			baseDao.setReturnInfo(message, "Y", "选题成功!");
		}
		return "confirmTask";
	}
	
	public String checkConfirmTask(){
		Map<String, Object> map = baseDao.queryTaskGroup(user_id);
		if (map != null) {//已经选过
			baseDao.setReturnInfo(message, "N", "已经选择课题："+map.get("TASK_NAME")+"!");
			return "confirmTask";
		}else {
			baseDao.setReturnInfo(message, "Y", "暂未选择课题");
			return "confirmTask";
		}
	}
	/**
	 *查询学院字典 
	 */
	public String queryFaculty(){
		try {
			List list = baseDao.queryAll("ESP_FACULTY");
			this.rows = baseDao.toJson(list);
			baseDao.setReturnInfo(message, "Y", "查询完毕");
			return "queryFaculty";
		} catch (Exception e) {
			baseDao.setReturnInfo(message, "N", "查询失败");
			return "queryFaculty";
		}
	}
	
	/**
	 *查询专业字典 
	 */
	public String queryMajor(){
		try {
			List list = baseDao.queryAll("ESP_Major");
			this.rows = baseDao.toJson(list);
			return "queryMajor";
		} catch (Exception e) {
			return "queryMajor";
		}
	}
	/**
	 *查询角色字典 
	 */
	public String queryRole(){
		try {
			List list = baseDao.querySqlForList("select * from esp_role where role_id in (2,3,4,6)");
			this.rows = baseDao.toJson(list);
			return "queryRole";
		} catch (Exception e) {
			return "queryRole";
		}
	}
	/**
	 * 增加
	 * @return
	 */
	public String addTask(){
		try {
			int i = baseDao.addTask(task_id, task_name, task_note, task_major_id, task_faculty_id, task_year);
			baseDao.setReturnInfo(message, "Y", "新增Topic数据"+i+"条");
			return "addTask";
		} catch (Exception e) {
			baseDao.setReturnInfo(message, "N", "增加数据异常");
			return "addTask";
		}
	}
	/**
	 * 修改
	 * @return
	 */
	public String updateTask(){
		try {
			int i = baseDao.updateTask(task_id, task_name, task_note, task_major_id, task_faculty_id, task_year);
			baseDao.setReturnInfo(message, "Y", "修改Topic数据"+i+"条");
			return "updateTask";
		} catch (Exception e) {
			baseDao.setReturnInfo(message, "N", "修改Topic异常");
			return "updateTask";
		}
	}
	/**
	 * 删除
	 * @return
	 */
	public String removeTask(){
		if (task_id == null) {
			return "removeTask";
		}
		int i = baseDao.removeTask(task_id);
		System.out.println("i=========================================="+i);
		if (i == 1) {
			baseDao.setReturnInfo(message, "Y", "删除成功!");
		}else{
			baseDao.setReturnInfo(message, "N", "删除失败,请确认此课题没被选择!");
		}
		return "removeTask";
	}
	
	
	public BaseDao getBaseDao() {
		return baseDao;
	}

	public void setBaseDao(BaseDao baseDao) {
		this.baseDao = baseDao;
	}

	public String getTask_id() {
		return task_id;
	}
	public void setTask_id(String taskId) {
		task_id = taskId;
	}
	public String getTask_name() {
		return task_name;
	}
	public void setTask_name(String taskName) {
		task_name = taskName;
	}
	public String getTask_note() {
		return task_note;
	}
	public void setTask_note(String taskNote) {
		task_note = taskNote;
	}
	public int getTask_year() {
		return task_year;
	}
	public void setTask_year(int taskYear) {
		task_year = taskYear;
	}
	@JSON(format="yy")
	public int getTaskYearStart() {
		return taskYearStart;
	}

	public void setTaskYearStart(int taskYearStart) {
		this.taskYearStart = taskYearStart;
	}
	@JSON(format="yy")
	public int getTaskYearEnd() {
		return taskYearEnd;
	}

	public void setTaskYearEnd(int taskYearEnd) {
		this.taskYearEnd = taskYearEnd;
	}
	
	public Map<String, String> getMessage() {
		return message;
	}
	public void setMessage(Map<String, String> message) {
		this.message = message;
	}
	public String getUser_id() {
		return user_id;
	}
	public void setUser_id(String userId) {
		user_id = userId;
	}
	public String getTask_faculty_id() {
		return task_faculty_id;
	}
	public void setTask_faculty_id(String taskFacultyId) {
		task_faculty_id = taskFacultyId;
	}
	public String getTask_major_id() {
		return task_major_id;
	}
	public void setTask_major_id(String taskMajorId) {
		task_major_id = taskMajorId;
	}
	public String getUser_role_id() {
		return user_role_id;
	}
	public void setUser_role_id(String userRoleId) {
		user_role_id = userRoleId;
	}
	public JSONArray getRows() {
		return rows;
	}
	public void setRows(JSONArray rows) {
		this.rows = rows;
	}
	
}
